package com.sc.nft.admin.rpc;

import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackCreateRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackDataRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackPageRequest;
import com.sc.nft.admin.request.ecologypool.EcologyPoolRedPackStatusRequest;
import com.sc.nft.entity.vo.EcologyPoolRedPackReceiveVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackCollectionVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackDataDetailVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackDetailVO;
import com.sc.nft.entity.vo.ecologypool.EcologyPoolRedPackPageVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyPoolRedPackService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/ecologyPool/redPack")
@Api(tags = "红包活动接口")
public class EcologyPoolRedPackRpc {

    private final EcologyPoolRedPackService ecologyPoolRedpackService;
    private final Redisson redisson;


    @ApiOperation(value = "红包列表", notes = "红包列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoRedPackPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<EcologyPoolRedPackPageVO> daoRedPackPage(@RequestBody EcologyPoolRedPackPageRequest request) {
        return PageResult.success(ecologyPoolRedpackService.daoRedPackPage(request.getDaoId(), request.getPageNo(), request.getPageSize(), request.getName(), request.getAuditStatus()));
    }


    @ApiOperation(value = "新增红包", notes = "新增红包", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/createRedPackPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Boolean> createRedPackPage(@RequestBody @Valid EcologyPoolRedPackCreateRequest request) {
        request.checkParticipateNft();
        request.checkNameLength();
        request.checkStartTime();
        RLock lock = redisson.getLock(String.format(RedisKeyEnum.CREATE_RED_PACK_LOCK.getKey(), request.getDaoId()));
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (!res) {
                throw new GlobalRunTimeException("太火爆了,请稍后再试");
            }
            ecologyPoolRedpackService.createRedPackPage(request.getDaoId(), request.getName(), request.getMechanism(), request.getType(), request.getRewardNumber(), request.getPackNumber(), request.getStartTime(), request.getEndTime(), request.getActiveRule(), request.getParticipate(), request.getParticipateNft(), request.getParticipateNumber());
        } catch (GlobalRunTimeException e) {
            log.error("新增红包异常", e);
            throw e;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }

    @ApiOperation(value = "红包详情", notes = "红包详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackPageDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<EcologyPoolRedPackDetailVO> redPackPageDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolRedpackService.redPackPageDetail(request.getId()));
    }

    @ApiOperation(value = "红包数据详情-分页", notes = "红包数据详情-分页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackDataPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<EcologyPoolRedPackReceiveVO> redPackDataPage(@RequestBody EcologyPoolRedPackDataRequest request) {
        return PageResult.success(ecologyPoolRedpackService.redPackDataPage(request.getPageNo(), request.getPageSize(), request.getId(), request.getUserTel()));
    }


    @ApiOperation(value = "数据详情-红包总额数据", notes = "数据详情-红包总额数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackDataDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<EcologyPoolRedPackDataDetailVO> redPackDataDetail(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolRedpackService.redPackDataDetail(request.getId()));
    }


    @ApiOperation(value = "开启关闭活动", notes = "开启关闭活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/redPackOpen", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Boolean> redPackOpen(@RequestBody @Valid EcologyPoolRedPackStatusRequest request) {
        ecologyPoolRedpackService.redPackOpen(request.getId(), request.getStatus());
        return Result.success();
    }

    @ApiOperation(value = "删除活动", notes = "删除活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/removeRedPack", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Boolean> removeRedPack(@RequestBody @Valid IdRequest request) {
        ecologyPoolRedpackService.removeRedPack(request.getId());
        return Result.success();
    }


    @ApiOperation(value = "获取dao下的藏品", notes = "获取dao下的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/listDaoDigitalCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<EcologyPoolRedPackCollectionVO>> listDaoDigitalCollection(@RequestBody @Valid IdRequest request) {
        return Result.success(ecologyPoolRedpackService.listDaoDigitalCollection(request.getId()));
    }

}
